package com.zsk.mybaits.demo.controller;

import com.zsk.mybaits.demo.dao.MTaskInfoMapper;
import com.zsk.mybaits.demo.entity.MTaskInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: keke
 * @date: 2021/6/12 0:14
 */
@RestController
public class DeadLockController {
    @Autowired
    MTaskInfoMapper taskDao;

    private static ExecutorService pool = Executors.newCachedThreadPool();

    @GetMapping("/testDeadLock")
    public String getTask() {
        for (int i = 1; i <= 100; i++) {
            final int ii = i;
            pool.execute(() -> {
                int state = (int) (Math.random() * 10);
                System.out.println("update " + ii + "state:" + state);
                MTaskInfo info = new MTaskInfo();
                info.setMId((long) ii);
                info.setTId((long) ii);
                info.setState(state);
                taskDao.updateByPrimaryKeySelective(info);
            });
        }
        return "test success";
    }

    @GetMapping("/insertTestData")
    public String insert() {
        int index = 1;
        for (int i = 1; i <= 10000; i++) {
            MTaskInfo info = new MTaskInfo();
            info.setMId((long) (i % 100));
            info.setTId((long) (index));
            if (i % 100 == 0) {
                index++;
            }
            taskDao.insert(info);
        }
        return "insert 10000 row data success";
    }
}
